Js字符串

2018.12.10 星期一 10:13

String: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String

语法

字符串字面量
String 函数将其他值生成或转换成字符串
### 模板字面量,
### 转义字符
### 长字符串
字符串写成多行: + 运算符;
其二,可以在每行末尾使用反斜杠字符(“\”),以指示字符串将在下一行继续。确保反斜杠后面没有空格或任何除换行符之外的字符或缩进; 否则反斜杠将不会工作

描述

### 从字符串中获取单个字符
charAt 方法,字符串当作一个类似数组的对象(数值索引)

### 字符串比较:比较操作符(>/</>=/<=)

基本字符串和字符串对象的区别

字符串字面量 (通过单引号或双引号定义) 和 直接调用 String 方法(没有通过 new 生成字符串对象实例)的字符串都是基本字符串。

当使用 eval时,基本字符串和字符串对象也会产生不同的结果。eval 会将基本字符串作为源代码处理; 而字符串对象则被看作对象处理, 返回对象。 例如:

1
2
3
4
5
6
7
8
9
10
11
// ## 1 String / new String
var s_prim = "foo"; // "foo"
var s_obj = new String(s_prim); // String {"foo"}
console.log(typeof s_prim); // "string"
console.log(typeof s_obj); // "object"

// ## 2 eval
s1 = "2 + 2"; // creates a string primitive
s2 = new String("2 + 2"); // creates a String object
console.log(eval(s1)); // returns the number 4
console.log(eval(s2)); // returns the string "2 + 2"

## 属性
String.prototype

方法

String.fromCharCode()
$PS:ES6
String.fromCodePoint():通过一串 码点 创建字符串。
String.raw():通过模板字符串创建字符串

## 字符串泛型方法

String 实例

### 属性
String.prototype.constructor:用于创造对象的原型对象的特定的函数。
String.prototype.length:返回了字符串的长度。
N:用于访问第N个位置的字符,其中N是小于 length 和 0之间的正整数。这些属性都是“只读”性质,不能编辑。

跟HTML无关的方法

String.prototype.charAt():返回特定位置的字符。
String.prototype.charCodeAt():返回表示给定索引的字符的Unicode的值。
String.prototype.normalize():返回调用字符串值的Unicode标准化形式。
String.prototype.quote() :设置嵌入引用的引号类型。

String.prototype.localeCompare():返回一个数字表示是否引用字符串在排序中位于比较字符串的前面,后面,或者二者相同。

String.prototype.indexOf():从字符串对象中返回首个被发现的给定值的索引值,如果没有找到则返回-1。
String.prototype.lastIndexOf():从字符串对象中返回最后一个被发现的给定值的索引值,如果没有找到则返回-1。

String.prototype.toLocaleLowerCase():根据当前区域设置,将符串中的字符转换成小写。对于大多数语言来说,toLowerCase的返回值是 一致的。
String.prototype.toLocaleUpperCase():根据当前区域设置,将字符串中的字符转换成大写,对于大多数语言来说,toUpperCase的返回值 是一致的。
String.prototype.toLowerCase():将字符串转换成小写并返回。
String.prototype.toUpperCase():将字符串转换成大写并返回。

String.prototype.toString():返回用字符串表示的特定对象。重写 Object.prototype.toString 方法。
String.prototype.toSource() :返回一个对象文字代表着特定的对象。你可以使用这个返回值来创建新的对象。重写Object.prototype.toSource 方法。 $PS:This API has not been standardized.
String.prototype.valueOf():返回特定对象的原始值。重写 Object.prototype.valueOf 方法。

String.prototype.trim():从字符串的开始和结尾去除空格。参照部分 ECMAScript 5 标准。
String.prototype.trimLeft() :从字符串的左侧去除空格。$PS:This API has not been standardized.
String.prototype.trimRight() :从字符串的右侧去除空格。$PS:This API has not been standardized.

String.prototype.concat():连接两个字符串文本,并返回一个新的字符串。
String.prototype.substring():返回在字符串中指定两个下标之间的字符。
String.prototype.substr():通过指定字符数返回在指定位置开始的字符串中的字符。
String.prototype.slice():摘取一个字符串区域,返回一个新的字符串。
String.prototype.split():通过分离字符串成字串,将字符串对象分割成字符串数组。

String.prototype.search():对正则表达式和指定字符串进行匹配搜索,返回第一个出现的匹配项的下标。
String.prototype.match():使用正则表达式与字符串相比较。
String.prototype.replace():被用来在正则表达式和字符串直接比较,然后用新的子串来替换被匹配的子串。

$PS: ES6
String.prototype.codePointAt():返回使用UTF-16编码的给定位置的值的非负整数。

String.prototype.includes():判断一个字符串里是否包含其他字符串。
String.prototype.startsWith():判断字符串的起始位置是否匹配其他字符串中的字符。
String.prototype.endsWith():判断一个字符串的结尾是否包含其他字符串中的字符。

String.prototype.repeat():返回指定重复次数的由元素组成的字符串对象。
String.prototype.padEnd():在当前字符串尾部填充指定的字符串, 直到达到指定的长度。 返回一个新的字符串。
String.prototype.padStart():在当前字符串头部填充指定的字符串, 直到达到指定的长度。 返回一个新的字符串。
$PS: ES6 end

String.prototype@@iterator:返回一个新的迭代器对象,该对象遍历字符串值的索引位置,将每个索引值作为字符串值返回。

## HTML wrapper methods
下面的方法被限制使用,因为只对可用的HTML标签和属性提供部分支持。
String.prototype.anchor()
String.prototype.link()
$PS:大多遗弃,比如:String.prototype.bold()

示例

### 将其他值转换成字符串
使用 String() 方法将其它对象转化为字符串可以被认为是一种更加安全的做法,虽然该方法底层使用的也是 toString() 方法,但是针对 null/undefined/symbols,String() 方法会有特殊的处理

$PS: toString

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Object.prototype.toString.call(null) // "[object Null]"
Object.prototype.toString.call(undefined) // "[object undefined]"

String(null) // "null"
String(undefined) // "null"

new String(null) // String {"null"}
new String(undefined) // String {"undefined"}
// ## Error
null.toString() //VM1110:1 Uncaught TypeError: Cannot read property 'toString' of null
new String(null).toString() // null
String(null).toString() // null


// # 数组的toString 方法
[1,2,3,[1,2,3]].toString() // "1,2,3,1,2,3"
[1,2,3,[1,2,3,[1,2,3]]].toString() // "1,2,3,1,2,3,1,2,3"
[1,2,3,[1,2,3],{a:2,b:2}].toString() // "1,2,3,1,2,3,[object Object]"

11:22

knowledge is no pay,reward is kindness
0%